package com.william.lifetraxer;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.william.lifetraxer.util.DbManager.*;

public class LogHandler extends HttpServlet {

	public LogHandler() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		String handleType = request.getParameter("log_handle_type");
		String username = request.getParameter("log_username");
		String password = request.getParameter("log_password");
		
		if("login".equals(handleType))
		{
			if(isLoginSuccess(username,password))
			{
				out.print("LOGIN_SUCCESS");
			}
			else
			{
				out.print("LOGIN_FAILED");
			}
		}
		else if("logout".equals(handleType))
		{
			if(isLogoutSuccess(username))
			{
				out.print("LOGOUT_SUCCESS");
			}
			else
			{
				out.print("LOGOUT_FAILED");
			}
		}
		else
		{
			out.print("VALID_LOG_HANDLE_TYPE");
		}
		
		out.close();
	}

	public void init() throws ServletException {

	}

}
